import { service } from './service'
import { useOutsideLoadingStore } from '@/store/loading'
import qs from 'qs'
const loading = useOutsideLoadingStore()

export const baseRequest = (url, value = {}, method = 'post', options = {}, showLoading = true) => {
  if(showLoading) {
    console.log(loading)
    loading.showLoading()
  }
  if(method === 'post') {
    return service.post(url, value, options)
  } else if (method === 'get') {
    return service.get(url, { params: value, ... options })
  } else if (method === 'formdata') {
    return service.post(url, qs.stringify(value), {
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      ...options
    })
  } else {
    return service({
      method: method, 
      url: url,
      data: value, 
      ...options
    })
  }
}

export const moduleRequest = (moduleUrl) => (url, ...arg) => {
  return baseRequest(moduleUrl + url, ...arg)
}

export const request = (url, ...arg) => {
  return baseRequest(url, ...arg)
}
